package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.missilike.biz.IMilAccountService;
import com.missilike.biz.IMilFollowRelationshipService;
import com.missilike.core.domain.DeleteFriendReuqest;
import com.missilike.core.exception.BizException;
import com.missilike.core.result.BizResultCode;
import com.missilike.domain.response.MilAccountInfo;
import com.missilike.repository.entity.MilAccount;
import com.missilike.repository.entity.MilFriendApply;
import com.missilike.repository.entity.MilFriendRelationship;
import com.missilike.repository.mapper.MilFriendRelationshipMapper;
import com.missilike.biz.IMilFriendRelationshipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pengpengli
 * @since 2018-12-19
 */
@Service
@Slf4j
public class MilFriendRelationshipServiceImpl extends ServiceImpl<MilFriendRelationshipMapper, MilFriendRelationship> implements IMilFriendRelationshipService {

    private IMilAccountService accountService;
    @Autowired
    private IMilFollowRelationshipService iMilFollowRelationshipService;


    @Override
    public List<MilAccountInfo> getAllFriends(MilAccount account) {
        if (StringUtils.isBlank(account.getId())) {
            return null;
        }
        return getAllFriends(account.getId());
    }

    @Override
    public List<MilAccountInfo> getAllFriends(String id) {
        QueryWrapper<MilFriendRelationship> friendsWrapper = new QueryWrapper();
        friendsWrapper.lambda().eq(MilFriendRelationship::getAccountId, id).or().eq(MilFriendRelationship::getFriendAccountId, id);
        List<MilFriendRelationship> friendRelationships = baseMapper.selectList(friendsWrapper);
        if (friendRelationships.size() == 0) {
            return new ArrayList<>();
        }
        Set<String> friendIds = friendRelationships.stream()
                .map(MilFriendRelationship::getFriendAccountId).filter(o -> !o.equals(id)).collect(Collectors.toSet());
        friendIds.addAll(
                friendRelationships.stream().map(MilFriendRelationship::getAccountId).filter(o -> !o.equals(id)).collect(Collectors.toSet())
        );
        List<MilAccountInfo> friends = accountService.getUserByIds(friendIds);
        return friends;
    }

    @Override
    public boolean addFriend(MilFriendRelationship relationship) {
        if (StringUtils.isBlank(relationship.getAccountId()) || StringUtils.isBlank(relationship.getFriendAccountId())
                || StringUtils.isBlank(relationship.getFriendAccountNickName()) || StringUtils.isBlank(relationship.getFriendAccountNickName())
                || relationship.getFriendAccount() == null || relationship.getFriendAccount() <= 0) {
            log.error("参数不合法，from:{},to:{}", relationship.getAccountId(), relationship.getFriendAccount());
            return false;
        }
        baseMapper.insert(relationship);
        return true;
    }

    @Override
    public void addToBlackList(String fromAccountId, String toAccountId) {
        QueryWrapper<MilFriendRelationship> relationshipQueryWrapper = new QueryWrapper<>();
        relationshipQueryWrapper.lambda().eq(MilFriendRelationship::getAccountId, fromAccountId)
                .eq(MilFriendRelationship::getFriendAccountId, toAccountId);
        MilFriendRelationship relationship = baseMapper.selectOne(relationshipQueryWrapper);
        if (null != relationship) {
            relationship.setIfBlacklist(true);
            baseMapper.updateById(relationship);
        }
    }

    @Override
    public boolean inBlackList(MilFriendApply apply) {
        QueryWrapper<MilFriendRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MilFriendRelationship::getAccountId, apply.getFromAccountId()).eq(MilFriendRelationship::getFriendAccountId, apply.getToAccountId())
                .eq(MilFriendRelationship::getIfBlacklist, true);
        List<MilFriendRelationship> relationship = baseMapper.selectList(queryWrapper);
        return relationship.size() > 0;
    }

    @Override
    public boolean isFriend(String id1, String id2) {
        QueryWrapper<MilFriendRelationship> friendsWrapper = new QueryWrapper();
        friendsWrapper.lambda().eq(MilFriendRelationship::getAccountId, id1).eq(MilFriendRelationship::getFriendAccountId, id2).eq(MilFriendRelationship::getIfBlacklist,false);
        if (baseMapper.selectList(friendsWrapper).size() > 0) {
            return true;
        }
        friendsWrapper = new QueryWrapper();
        friendsWrapper.lambda().eq(MilFriendRelationship::getAccountId, id2).eq(MilFriendRelationship::getFriendAccountId, id1).eq(MilFriendRelationship::getIfBlacklist,false);
        if (baseMapper.selectList(friendsWrapper).size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void setAccountService(MilAccountServiceImpl milAccountService) {
        this.accountService = milAccountService;
    }

    public boolean deleteFriend(DeleteFriendReuqest reuqest) {
        MilFriendRelationship milFriendRelationship = getOne(new QueryWrapper<MilFriendRelationship>().
                lambda().eq(MilFriendRelationship::getAccountId, reuqest.getAccountId()).
                eq(MilFriendRelationship::getFriendAccountId, reuqest.getFriendAccountId()));

        MilFriendRelationship milFriendRelationship2 = getOne(new QueryWrapper<MilFriendRelationship>().
                lambda().eq(MilFriendRelationship::getAccountId, reuqest.getFriendAccountId()).
                eq(MilFriendRelationship::getFriendAccountId, reuqest.getAccountId()));

        if (milFriendRelationship == null & milFriendRelationship2 == null) {
            throw new BizException(BizResultCode.NO_FRIENDS);
        }

        if (milFriendRelationship != null && milFriendRelationship.getId() > 0) {
            baseMapper.deleteById(milFriendRelationship.getId());
            iMilFollowRelationshipService.deleteMutualConcern(milFriendRelationship.getAccountId(), milFriendRelationship.getFriendAccountId());
        }

        if (milFriendRelationship2 != null && milFriendRelationship2.getId() > 0) {
            baseMapper.deleteById(milFriendRelationship2.getId());
            iMilFollowRelationshipService.deleteMutualConcern(milFriendRelationship2.getAccountId(), milFriendRelationship2.getFriendAccountId());
        }
        return true;
    }
}
